/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.editor;

import easybooking.EBInterface;
import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author Tim Schröder
 */
public class EBMenuTree extends JTree {

    private static EBMenuTree EasyBookingMenuTree = null;
    private static EBInterface ebi;


    private EBMenuTree(TreeNode root) {
        // Passes the nodes to the super class
        super(root);
    }

    // Singelton implementation
    public static EBMenuTree EasyBookingMenuTree(EBInterface ebif) {
        ebi = ebif;
        if (EasyBookingMenuTree == null) {
            EBMenuTree tree = new EBMenuTree(EasyBookingMenuTree.createNodes());
            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            tree.addTreeSelectionListener(ebi);

            return tree;
        }
        return EasyBookingMenuTree;
    }

    // creates nodes of the tree
    /**
     * creates nodes of the tree
     * @return a TreeNode Object which sets all nodes for EBMenuTree Object
     */
    private static TreeNode createNodes() {

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("EasyBooking");

        {

            DefaultMutableTreeNode bookingNodes = new DefaultMutableTreeNode("Bookings");
            root.add(bookingNodes);

            DefaultMutableTreeNode flightNodes = new DefaultMutableTreeNode("Flights");
            root.add(flightNodes);

            DefaultMutableTreeNode customerNodes = new DefaultMutableTreeNode("Customers");
            root.add(customerNodes);
            
            DefaultMutableTreeNode airlineNodes = new DefaultMutableTreeNode("Airlines");
            root.add(airlineNodes);          

            DefaultMutableTreeNode airportNodes = new DefaultMutableTreeNode("Airports");
            root.add(airportNodes);

             DefaultMutableTreeNode planeNodes = new DefaultMutableTreeNode("Planes");
            root.add(planeNodes);

            DefaultMutableTreeNode typeNodes = new DefaultMutableTreeNode("Types");
            root.add(typeNodes);




        }
        return root;

    }

    // http://www.java2s.com/Code/Java/Swing-JFC/ExpandingorCollapsingAllNodesinaJTreeComponent.htm
    public void expandAll() {
        TreeNode root = (TreeNode) this.getModel().getRoot();
        expandAll(this, new TreePath(root));
    }

    private void expandAll(JTree tree, TreePath parent) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path);
            }
        }
        tree.expandPath(parent);
        // tree.collapsePath(parent);
    }
}
